package com.ywjh.inter.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ywjh.inter.common.MemKey;
import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.conn.MemcacheConn;
import com.ywjh.inter.utils.HTTPHelper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class HXService {
	private static HXService service;

	private static String url_head = SystemConfig.HX_URL + "/" + SystemConfig.ORI + "/" + SystemConfig.APP_NAME;

	public static HXService getInstance() {
		if (service == null) {
			service = new HXService();
		}
		return service;
	}

	public String token() throws IOException {
		String token = null;
		if (MemcacheConn.getInstance().get(MemKey.HX_TOKEN) != null) {
			token = MemcacheConn.getInstance().get(MemKey.HX_TOKEN);
		} else {
			JSONObject tokenj = getToken(SystemConfig.CLIENT_ID, SystemConfig.CLIENT_SECERT);
			token = tokenj.getString("access_token");
			int expires = tokenj.getInt("expires_in");
			MemcacheConn.getInstance().put(MemKey.HX_TOKEN, token, expires - 60);
		}
		return token;
	}

	// 获得tocken
	public JSONObject getToken(String clientId, String secret) throws IOException {
		String url = url_head + "/token";
		JSONObject json = new JSONObject();
		json.put("grant_type", "client_credentials");
		json.put("client_id", clientId);
		json.put("client_secret", secret);
		String s = HTTPHelper.postData(url, json, null);
		JSONObject result = JSONObject.fromObject(s);
		return result;
	}

	// 注册IM
	public JSONObject regiestUser(String username, String password, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users";
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		JSONObject json = new JSONObject();
		json.put("username", username);
		json.put("password", password);
		String s = HTTPHelper.postData(url, json, header);
		result = JSONObject.fromObject(s);
		return result;
	}

	// 添加好友
	public JSONObject addFriend(String u1, String u2, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + u1 + "/contacts/users/" + u2;
		System.out.println("add_friend=="+url);
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		JSONObject json = new JSONObject();
		String s = HTTPHelper.postData(url, json, header);
		result = JSONObject.fromObject(s);
		System.out.println("add_friend==result："+result);
		return result;
	}

	// 获得黑名单
	public JSONObject getBlacklist(String username, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + username + "/blocks/users";
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		String s = HTTPHelper.getData(url, header);
		result = JSONObject.fromObject(s);
		return result;
	}

	// 获得好友列表
	public JSONObject getFriendList(String username, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + username + "/contacts/users";
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		String s = HTTPHelper.getData(url, header);
		result = JSONObject.fromObject(s);
		System.out.println("getFriendList=="+result);
		return result;
	}
	// 获得离线消息数
	public JSONObject getOfflineMSGCount(String username, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + username + "/offline_msg_count";
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		String s = HTTPHelper.getData(url, header);
		result = JSONObject.fromObject(s);
		return result;
	}

	// 获取用户
	public JSONObject getUsers(String username, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + username;
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		String s = HTTPHelper.getData(url, header);
		result = JSONObject.fromObject(s);
		return result;
	}

	// 添加黑名单
	public JSONObject AddBlockUser(String username, List<String> blockList, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + username + "/blocks/users";
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		JSONArray array = new JSONArray();
		for (String u : blockList) {
			array.add(u);
		}
		JSONObject post = new JSONObject();
		post.put("usernames", array);
		String s = HTTPHelper.postData(url, post, header);
		result = JSONObject.fromObject(s);
		return result;
	}

	// 恢复黑名单
	public JSONObject deleteBlockUser(String username, String u1, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + username + "/blocks/users/" + u1;
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		String s = HTTPHelper.deleteData(url, header);
		result = JSONObject.fromObject(s);
		return result;
	}

	// 删除好友
	public JSONObject deleteFriend(String username, String u1, String token) throws IOException {
		JSONObject result = null;
		String url = url_head + "/users/" + username + "/contacts/users/" + u1;
		Map<String, String> header = new HashMap<>();
		header.put("Authorization", "Bearer " + token);
		String s = HTTPHelper.deleteData(url, header);
		result = JSONObject.fromObject(s);
		return result;
	}
	
	public static void main(String[] args) throws IOException {

		String d="user_33";
		JSONObject  tokenj=getInstance().getToken(SystemConfig.CLIENT_ID, SystemConfig.CLIENT_SECERT);
		String token = tokenj.getString("access_token");
		System.out.println(token);
//		for(String p:list) {
//			System.out.println(p+"==>"+d);
//			getInstance().addFriend(d, p, token);
//		}
		
		JSONObject friend=getInstance().getFriendList(d, token);
		System.out.println(friend.toString());
	}

}
